<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">











<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <title>jxls - Dynamic worksheets</title>
    <style type="text/css" media="all">
      @import url("../css/maven-base.css");
      @import url("../css/maven-theme.css");
      @import url("../css/site.css");
    </style>
    <link rel="stylesheet" href="../css/print.css" type="text/css" media="print" />
          <meta name="author" content="Leonid Vysochyn" />
        <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
      </head>
  <body class="composite">
    <div id="banner">
                  <a href="http://jxls.sourceforge.net" id="bannerLeft">
    
            jXLS
    
            </a>
                          <span id="bannerRight">
    
                                    <img src="http://sourceforge.net/sflogo.php?group_id=141729&type=1" alt="" />
    
            </span>
            <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="breadcrumbs">
          
  

  
    
  
  
    
            <div class="xleft">
        Last Published: 13 Nov 2014
                      </div>
            <div class="xright">            <a href="http://sourceforge.net/projects/jxls" class="externalLink">jXLS on Sourceforge</a>
            
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
    <div id="leftColumn">
      <div id="navcolumn">
           
  

  
    
  
  
    
                   <h5>Overview</h5>
            <ul>
              
    <li class="none">
                    <a href="../index.html">Home</a>
          </li>
              
    <li class="none">
                    <a href="http://sourceforge.net/project/showfiles.php?group_id=141729" class="externalLink">Downloads</a>
          </li>
              
    <li class="none">
                    <a href="../reference/installation.html">Installation</a>
          </li>
          </ul>
              <h5>Reference</h5>
            <ul>
              
    <li class="none">
                    <a href="../reference/simplebeans.html">Simple Beans</a>
          </li>
              
    <li class="none">
                    <a href="../reference/tags.html">Using Tags</a>
          </li>
              
    <li class="none">
                    <a href="../reference/sqlreporting.html">SQL Reporting</a>
          </li>
              
    <li class="none">
                    <a href="../reference/collections.html">Collections</a>
          </li>
              
    <li class="none">
                    <a href="../reference/advancedcollections.html">Advanced Collections</a>
          </li>
              
    <li class="none">
              <strong>Dynamic Sheets</strong>
        </li>
              
    <li class="none">
                    <a href="../reference/expressions.html">Using Expressions</a>
          </li>
              
    <li class="none">
                    <a href="../reference/formulas.html">Formulas Notation</a>
          </li>
              
    <li class="none">
                    <a href="../reference/poiaccess.html">POI Objects Access</a>
          </li>
              
    <li class="none">
                    <a href="../reference/columnhiding.html">Column Hiding</a>
          </li>
              
    <li class="none">
                    <a href="../reference/sheetoperations.html">Spreadsheet Operations</a>
          </li>
              
    <li class="none">
                    <a href="../reference/customprocessor.html">Custom Processor</a>
          </li>
              
    <li class="none">
                    <a href="../reference/resultset.html">JDBC ResultSet</a>
          </li>
              
    <li class="none">
                    <a href="../reference/reader.html">XLS Reader</a>
          </li>
          </ul>
              <h5>Samples</h5>
            <ul>
              
    <li class="none">
                    <a href="../samples/tagsample.html">Tags Sample</a>
          </li>
              
    <li class="none">
                    <a href="../samples/chartsample.html">Charts</a>
          </li>
              
    <li class="none">
                    <a href="../samples/reportsample.html">Reporting Sample</a>
          </li>
              
    <li class="none">
                    <a href="../samples/collectionsample.html">Collections</a>
          </li>
              
    <li class="none">
                    <a href="../samples/adjacentlists.html">Adjacent Lists</a>
          </li>
              
    <li class="none">
                    <a href="../samples/dynamicstylesample.html">Dynamic Style</a>
          </li>
              
    <li class="none">
                    <a href="../samples/resultsetsample.html">JDBC ResultSet</a>
          </li>
              
    <li class="none">
                    <a href="../samples/dynamiccolumnssample.html">Dynamic Columns</a>
          </li>
          </ul>
              <h5>Project Documentation</h5>
            <ul>
              
                
              
      
            
      
            
      
              
        <li class="collapsed">
                    <a href="../project-info.html">Project Information</a>
                </li>
          </ul>
                                           <a href="http://maven.apache.org/" title="Built by Maven" class="poweredBy">
            <img alt="Built by Maven" src="../images/logos/maven-feather.png"></img>
          </a>
                       
  

  
    
  
  
    
        </div>
    </div>
    <div id="bodyColumn">
      <div id="contentBox">
        <div class="section"><h2><a name="Export_of_a_single_collection_into_multiple_worksheets"></a>Export of a single collection into multiple worksheets</h2>
<p>Sometimes it is required to export every collection item into separate worksheet. jXLS provides
            a special way to do this.</p>
<p>Let's look at the following method of XLSTransformer class</p>
<div class="source"><pre>
 public HSSFWorkbook transformMultipleSheetsList(InputStream is, List objects, List newSheetNames, String beanName, Map beanParams, int startSheetNum)
            </pre>
</div>
<p>Here</p>
<div class="source"><pre>
 is             - InputStream for source XLS template file
 objects        - List of beans where each list item should be exported into a separated worksheet
 newSheetNames  - Sheet names to be used for newly created worksheets
 beanName       - Bean name to be used for a list item when processing sheet
 beanParams     - Common bean map containing all other objects to be used in the workbook
 startSheetNum  - Worksheet number (zero based) of the worksheet that needs to be used to create multiple worksheets
            </pre>
</div>
<p>Using of this method is simple. Here is an example</p>
<div class="source"><pre>
InputStream is = new BufferedInputStream(new FileInputStream(&quot;multipleSheetList.xls&quot;));
XLSTransformer transformer = new XLSTransformer();
List sheetNames = new ArrayList();
for(int i = 0; i &lt; departments.size(); i++){
    Department department = (Department) departments.get( i );
    sheetNames.add( department.getName() );
}
HSSFWorkbook resultWorkbook = transformer.transformMultipleSheetsList(is, departments, sheetNames, &quot;department&quot;, new HashMap(), 0);
            </pre>
</div>
<p>In this example we export each department into a separate worksheet using the first worksheet as
                the main template for a single department export. Every sheet will be named as corresponding department name.</p>
<p>You can also export many objects per each worksheet. This can be done using Maps. Just put all the objects that needs to be accessed
            from the corresponding worksheet into a Map. Create a List of such Map objects (one Map per each Worksheet). Then
            pass it to transformMultipleSheetsList() method.</p>
<p>In your template you have to access each object via Map using map values access notation ${map.get('object1').prop1}. As an example take a look at the following code
            which demonstrates this by exporting every Department object into a separate worksheet</p>
<div class="source"><pre>
                List sheetNames = new ArrayList();
                List maps = new ArrayList();
                for(int i = 0; i &lt; departments.size(); i++){
                    Map map = new HashMap();
                    Department department = (Department) departments.get( i );
                    map.put(&quot;department&quot;, department );
                    sheetNames.add( department.getName() );
                    map.put(&quot;name&quot;, &quot;Number &quot; + i);
                    maps.add( map );
                }
                XLSTransformer transformer = new XLSTransformer();
                org.apache.poi.ss.usermodel.Workbook resultWorkbook = transformer.transformMultipleSheetsList(inputStream, maps, sheetNames, &quot;map&quot;, new HashMap(), 0);                
            </pre>
</div>
</div>

      </div>
    </div>
    <div class="clear">
      <hr/>
    </div>
    <div id="footer">
      <div class="xright">&#169;  
          2005-2014
    
          jXLS Team
          
  

  
    
  
  
    
  </div>
      <div class="clear">
        <hr/>
      </div>
    </div>
  </body>
</html>
